package com.juku2024.juku.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.juku2024.juku.R
import com.juku2024.juku.VideoDetailActivity
import com.juku2024.juku.adapter.ThemeDetailItemAdapter
import com.juku2024.juku.base.BaseFragment
import com.juku2024.juku.model.CommonModel
import com.juku2024.juku.tools.LogC
import com.juku2024.juku.viewmodel.UnlockViewModel
import com.juku2024.juku.widget.LoadingView


class RankingListFragment : BaseFragment() {
    
    private lateinit var recyclerView: RecyclerView 
    private lateinit var loadingView: LoadingView

    private var genreId = -1
    private val rankingAdapter by lazy { ThemeDetailItemAdapter(ArrayList()) }
    private val viewModel by lazy { getViewModel(requireActivity().viewModelStore, UnlockViewModel::class.java) }

    override fun layoutView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_ranking_list, container, false)
    }

    override fun initView(rootView: View) {
        super.initView(rootView)
        recyclerView = rootView.findViewById(R.id.rank_detail_recyclerView)
        loadingView = rootView.findViewById(R.id.rank_detail_loadingView)
        
        
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        rankingAdapter.isShowIcon = true
        recyclerView.adapter = rankingAdapter
    }

    override fun bindEvent() {
        super.bindEvent()
        rankingAdapter.setOnItemClickListener { _, _, position ->
            VideoDetailActivity.toVideoDetail(requireContext(), rankingAdapter.data[position].id, rankingAdapter.data[position].name)
        }
    }

    @Suppress("DEPRECATION")
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        genreId = arguments?.getInt("ranking_id") ?: -1
        val data = arguments?.getParcelableArrayList<CommonModel>("ranking_data")
        if (data.isNullOrEmpty()) {
            loadingView.showLoading()
            doHttpTabData()
        } else {
            LogC.e("使用缓存数据...")
            rankingAdapter.setList(data)
        }
    }



    private fun doHttpTabData() {
        val startTime = System.currentTimeMillis()
        viewModel.getRankingNavDetailList(genreId, {
            if (isFragmentNotActive()) return@getRankingNavDetailList
            loadingView.hideLoading()
            if (it.videos.isNullOrEmpty()) {
                loadingView.showEmptyStatus(getString(R.string.empty_list))
                return@getRankingNavDetailList
            }
            val httpTime = System.currentTimeMillis() - startTime
            if (httpTime < 500) {
                loadingView.postDelayed({
                    rankingAdapter.setList(it.videos)
                }, 500 - httpTime)
            } else {
                rankingAdapter.setList(it.videos)
            }
        }, {
            if (isFragmentNotActive()) return@getRankingNavDetailList
            loadingView.hideLoading()
            loadingView.showErrorStatus(it, getString(R.string.retry)) {
                loadingView.showLoading()
                loadingView.postDelayed({
                    doHttpTabData()
                }, 500)
            }
        })
    }


    override fun onDestroyView() {
        super.onDestroyView()
        rankingAdapter.setList(ArrayList())
    }

}